<template>
  <a-drawer
    :title="title"
    :maskClosable="true"
    :width="drawerWidth"
    placement="right"
    :closable="true"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @close="handleCancel"
    style="height: 100%; overflow: auto; padding-bottom: 53px"
  >
    <template slot="title">
      <div style="width: 100%">
        <span>{{ title }}</span>
        <span style="display: inline-block; width: calc(100% - 51px); padding-right: 10px; text-align: right">
          <a-button @click="toggleScreen" icon="appstore" style="height: 20px; width: 20px; border: 0px"></a-button>
        </span>
      </div>
    </template>

    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="packageTypeStatus"
          required
          label="体验式服务"
        >
          <a-switch
            checkedChildren="是"
            unCheckedChildren="否"
            v-model="model.packageTypeStatus"
            @change="handleSetPackageType"
          />
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="packageName" required label="服务包名称">
          <a-input placeholder="请输入服务包名称" :max-length="20" v-model="model.packageName" />
        </a-form-model-item>

        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="packageServicePeriod" label="服务周期">
          <a-select
            v-model="model.packageServicePeriod"
            placeholder="请选择服务周期"
            :getPopupContainer="target => target.parentNode"
          >
            <a-select-option :value="item.value" v-for="item in periodList" :key="item.value">{{
              item.label
            }}</a-select-option>
          </a-select>
          <!-- <j-multi-select-tag
            v-model="model.packageServicePeriod"
            :options="periodList"
            placeholder="请选择服务周期">
          </j-multi-select-tag> -->
        </a-form-model-item>

        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="packagePrice" label="原价">
          <a-input-number placeholder="请输入原价" v-model="model.packagePrice" style="width: 100%" />
        </a-form-model-item>

        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="packageDiscount" label="折扣价">
          <a-input-number placeholder="请输入折扣价" v-model="model.packageDiscount" style="width: 100%" />
        </a-form-model-item>

        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="packageDesc" label="服务包描述">
          <a-input placeholder="请输入服务包描述" type="textarea" :max-length="30" v-model="model.packageDesc" />
        </a-form-model-item>
        <!-- <template v-if="model.packageType == 2"> -->
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="effect">
          <label class="required" slot="label">管理成效</label>
          <div
            :class="manageEffect.length > 1 ? 'manage-group' : 'manage-group normalWidth'"
            v-for="(item, index) in manageEffect"
            :key="index"
          >
            <a-input placeholder="请输入管理成效" :max-length="10" v-model="item.effectName" />
            <img
              class="delete"
              v-if="manageEffect.length > 1"
              src="../../../assets/delete.png"
              @click="removeDomain(index)"
            />
          </div>
        </a-form-model-item>
        <a-form-model-item :wrapperCol="addWrapperCol">
          <div class="add-icon" @click="handleAddEffect">
            <a-icon type="plus" />
          </div>
        </a-form-model-item>
        <!-- <template v-if="model.packageType == 2"> -->
        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="packageThumb"
          :rules="[{ required: true, message: '请上传缩略图' }]"
          label="缩略图"
          extra="请上传尺寸为500px*500px的图片"
        >
          <j-image-upload v-model="model.packageThumb"></j-image-upload>
        </a-form-model-item>
        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="packageSlideshow"
          :rules="[{ required: true, message: '请上传轮播图' }]"
          label="轮播图"
          extra="请上传尺寸为500px*500px的图片"
        >
          <j-image-upload v-model="model.packageSlideshow" :is-multiple="true" :number="4"></j-image-upload>
        </a-form-model-item>
        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="packageDetail"
          :rules="[{ required: true, message: '请上传详情海报' }]"
          label="详情海报"
          extra="请上传宽度为750px的图片"
        >
          <j-image-upload v-model="model.packageDetail" :is-multiple="false"></j-image-upload>
        </a-form-model-item>
        <!-- </template> -->
      </a-form-model>
    </a-spin>

    <div class="drawer-bootom-button" v-show="!disableSubmit">
      <a-popconfirm title="确定放弃编辑？" @confirm="handleCancel" okText="确定" cancelText="取消">
        <a-button style="margin-right: 0.8rem">取消</a-button>
      </a-popconfirm>
      <a-button @click="handleOk" type="primary" :loading="confirmLoading">提交</a-button>
    </div>
  </a-drawer>
</template>

<script>
import Vue from 'vue'
import { USER_INFO } from '@/store/mutation-types'
import { postAction } from '@/api/manage'
import { mapGetters } from 'vuex'

export default {
  name: 'PackageModal',
  data() {
    return {
      periodList: [],
      weekList: [
        {
          value: 1,
          label: '一周'
        },
        {
          value: 2,
          label: '两周'
        },
        {
          value: 3,
          label: '三周'
        },
        {
          value: 4,
          label: '四周'
        }
      ],
      monthList: [
        {
          value: 1,
          label: '一个月'
        },
        {
          value: 2,
          label: '二个月'
        },
        {
          value: 3,
          label: '三个月'
        },
        {
          value: 4,
          label: '四个月'
        },
        {
          value: 5,
          label: '五个月'
        },
        {
          value: 6,
          label: '六个月'
        },
        {
          value: 7,
          label: '七个月'
        },
        {
          value: 8,
          label: '八个月'
        },
        {
          value: 9,
          label: '九个月'
        },
        {
          value: 10,
          label: '十个月'
        },
        {
          value: 11,
          label: '十一个月'
        },
        {
          value: 12,
          label: '十二个月'
        }
      ],
      drawerWidth: 700,
      disableSubmit: false,
      manageEffect: [{ effectName: '', effectId: 1 }],
      value: 1,
      title: '操作',
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      addWrapperCol: {
        xs: { span: 24 },
        sm: { span: 16, offset: 5 }
      },
      confirmLoading: false,
      validatorRules: {
        packageName: [{ required: true, message: '请输入服务包名称!' }],
        packageServicePeriod: [{ required: true, message: '请选择服务周期!' }],
        effect: [{ validator: this.hasEffect }],
        packagePrice: [{ required: true, message: '请输入原价!' }],
        packageDiscount: [{ required: true, message: '请输入折扣价!' }],
        packageDesc: [{ required: true, message: '请输入服务包描述!' }]
      }
    }
  },
  created() {
    console.log(this.manageEffect)
  },
  methods: {
    ...mapGetters(['doctorId', 'orgCode']),
    add() {
      this.edit({ packageType: 1 })
    },
    edit(record) {
      record.packageTypeStatus = record.packageType == 1 ? false : true
      this.model = Object.assign({}, record)
      this.model.manageEffect && (this.manageEffect = this.model.manageEffect)
      console.log(this.model)
      this.periodList = record.packageType == 1 ? this.monthList : this.weekList
      // this.handleSetPackageType(this.model.packageType)
      this.visible = true
      this.resetScreenSize()
    },
    input() {
      console.log(this.model.manageEffect[0].value)
    },
    handleSetPackageType(checked) {
      if (checked) {
        this.model.packageType = 2
        this.periodList = this.weekList
      } else {
        this.model.packageType = 1
        this.periodList = this.monthList
      }
      this.model.packageServicePeriod && (this.model.packageServicePeriod = null)
    },
    // 根据屏幕变化,设置抽屉尺寸
    resetScreenSize() {
      let screenWidth = document.body.clientWidth
      if (screenWidth < 500) {
        this.drawerWidth = screenWidth
      } else {
        this.drawerWidth = 700
      }
    },
    handleAddEffect() {
      const ids = this.manageEffect.map(v => {
        return v.effectId
      })
      const id = ids.sort()[ids.length - 1] + 1
      console.log(ids)
      this.manageEffect.push({ effectName: '', effectId: id })
    },
    removeDomain(domin) {
      this.manageEffect.splice(domin, 1)
    },
    //窗口最大化切换
    toggleScreen() {
      if (this.modaltoggleFlag) {
        this.modalWidth = window.innerWidth
      } else {
        this.modalWidth = 800
      }
      this.modaltoggleFlag = !this.modaltoggleFlag
    },
    // 确定
    handleOk() {
      const that = this
      this.model.manageEffect = this.manageEffect
      // 触发表单验证
      that.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let model = JSON.parse(JSON.stringify(that.model))
          model.packageType = model.packageType == true ? 1 : 2
          console.log(model)
          model = Object.assign(model, { doctorId: this.doctorId(), orgCode: this.orgCode() })

          postAction('107/03/1070310', model)
            .then(res => {
              console.log('res=======', res)
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        } else {
          return false
        }
      })
    },
    hasEffect(rule, value, callback) {
      if (this.manageEffect[0].effectName) {
        const list = this.manageEffect.map(v => {
          return v.effectName
        })
        const arr1 = [...new Set(list)]
        console.log(arr1, list)
        if (arr1.length < list.length) {
          callback(new Error('管理成效重复!'))
        } else {
          callback()
        }
      } else {
        callback(new Error('请输入管理成效'))
      }
    },
    // 关闭
    handleCancel() {
      this.close()
    },
    close() {
      this.$emit('close')
      this.manageEffect = [{ effectName: '', effectId: 1 }]
      this.visible = false
      this.confirmLoading = false
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style scoped>
.avatar-uploader > .ant-upload {
  width: 104px;
  height: 104px;
}
.ant-upload-select-picture-card i {
  font-size: 49px;
  color: #999;
}

.ant-upload-select-picture-card .ant-upload-text {
  margin-top: 8px;
  color: #666;
}

.ant-table-tbody .ant-table-row td {
  padding-top: 10px;
  padding-bottom: 10px;
}

.drawer-bootom-button {
  position: absolute;
  bottom: -8px;
  width: 100%;
  border-top: 1px solid #e8e8e8;
  padding: 10px 16px;
  text-align: right;
  left: 0;
  background: #fff;
  border-radius: 0 0 2px 2px;
}
.add-icon {
  width: 100%;
  height: 40px;
  background: #fafafa;
  border-radius: 4px;
  border: 1px dashed #d9d9d9;
  text-align: center;
  margin-top: 10px;
}
.required::before {
  display: inline-block;
  margin-right: 4px;
  color: #f5222d;
  font-size: 14px;
  font-family: SimSun, sans-serif;
  line-height: 1;
  content: '*';
}
.normalWidth {
  width: 100% !important;
}
.manage-group {
  display: flex;
  align-items: center;
  width: calc(100% + 30px);
  margin-bottom: 10px;
}
.delete {
  width: 20px;
  height: 20px;
  display: inline-block;
  margin-left: 10px;
  cursor: pointer;
}
</style>

